<?php

class DefaultController extends BookController
{
	public $bookName='';
	public $nav = '&nbsp;';
	public $bookList = '<a href="/">返回图书列表</a>';
	public $expressNav = '&nbsp;';
	
	
	public $metaKeywords = '';
	public $metaDescription = '';
	
	public $layout='/layouts/main';

	public function filters()
	{
		return array(
			array(
			        'CHttpCacheFilter + index,chapter',
			        'lastModifiedExpression'=>array($this,'setLastModified'),
			        'etagSeedExpression'=>array($this,'setEtag')
			),
			array(
				'COutputCache + index,chapter',
				'duration'=>172800,
				'id'=>'book_default',
				'varyByExpression'=>array($this,'cacheExpression')
			)
		);
	}

	/**
	 * 页面缓存 回调
	 */
	public function cacheExpression() {
		$bookFlag = Yii::app()->request->getQuery('bookFlag');
		$actionId = Yii::app()->getController()->getAction()->getId();
		$lang = isset($_GET['lang']) && $_GET['lang'] == 'en' ? 'en' : 'zh';
		switch($actionId) {
			case 'chapter':
				$chapterFlag = Yii::app()->request->getQuery('chapterFlag');
				return $bookFlag.'-'.$chapterFlag.'-'.$lang;
				break;
			default:
				return $bookFlag.'-'.$lang;
		}
		return '';
	}
	
	
	/**
	 * 目录
	 * @param string $flag  书名标识
	 */
	public function actionIndex($bookFlag, $lang='zh')
	{
		if ($bookFlag=='') {
			$this->layout = false;
			$error = array();
			$this->render('error404', $error);
			exit;
		}
        
		$book = $this->getBookInfo($bookFlag);
        
		if ($lang == 'en') {
                	$this->pageTitle = $book->originalBookName .' - '. Yii::app()->name;
                	$this->metaKeywords = $book->metaKeywords;
                	$this->metaDescription = $book->metaDescription;
                	$this->bookName =  $book->originalBookName;
		}
		else {
			$this->pageTitle = $book->bookName .' - '. Yii::app()->name;
			$this->metaKeywords = $book->metaKeywords;
			$this->metaDescription = $book->metaDescription;
			$this->bookName =  $book->bookName;
		}
		$tableOfContents = $this->getBookTableOfContents($book->moduleId);
                
		$view =  ($lang == 'en') ? "index_en" : "index";

		$this->render($view, array(
				'bookFlag'=>$bookFlag,
				'bookId'=>$book->moduleId,
				'tableOfContents'=>$tableOfContents,
				'lang' => $lang
		));
	}

	public function actionShowMb($bookFlag, $chapterFlag, $lang="zh")
	{
		$this->layout=false;
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		$chapterId = $chapter['chapterId'];
		$sections = $this->getSectionList($book->moduleId, $chapterId);

		$this->render('mb', array(
				'chapterName'=>($lang == 'en') ? $chapter['originalChapterName'] : $chapter['chapterName'],
				'sections'=>$sections
		));
	}

	/**
	 * 章节
	 * @param string $flag  书名标识
	 * @param string $chapter 章节标识
	 */
	public function actionChapter($bookFlag, $chapterFlag, $lang="zh") {
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		$chapterId = $chapter['chapterId'];

		$this->createNav($bookFlag, $chapter, $lang);
		
		if ($lang == 'en') {
			$this->pageTitle = $chapter['originalChapterName'] .' - '. $book->originalBookName .' - '. Yii::app()->name;
			$this->metaKeywords = $chapter['metaKeywords'];
			$this->metaDescription = $chapter['metaDescription'];
			$this->bookName =  $book->originalBookName;
		}
		else {
			$this->pageTitle = $chapter['chapterName'] .' - '. $book->bookName .' - '. Yii::app()->name;
			$this->metaKeywords = $chapter['metaKeywords'];
			$this->metaDescription = $chapter['metaDescription'];
			$this->bookName =  $book->bookName;
		}
		
		if ($chapter['sectionShow']==0) {
			$this->render('comesoon', array('chapterName'=>$chapter['chapterName']));
			exit;
		}

		
		$sections = $this->getSectionList($book->moduleId, $chapterId);

		$view =  ($lang == 'en') ? "chapter_en" : "chapter";
		$this->render($view, array(
				'chapterName'=>($lang == 'en') ? $chapter['originalChapterName'] : $chapter['chapterName'],
				'expressNavContent'=>$this->createExpressNavContent($book->moduleId, $chapterId, $lang),
				'sections'=>$sections
		));
	}
	
	/**
	 * 页面访问记录
	 */
	public function actionHits() {
		if (isset($_SERVER['HTTP_REFERER'])) {
			
			$data = Yii::app()->cache->get($_SERVER['HTTP_REFERER']);
			if ($data==false) {
				$urls = parse_url($_SERVER['HTTP_REFERER']);
				if (isset($urls['path'])) {
					$paths = explode('/',  $urls['path']);
					if ($paths[1]=='book') {
						$bookFlag = $paths[2];
						$chapterFlag = $paths[3];
						$book = $this->getBookInfo($bookFlag);
						$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
						$data = array($book->moduleId, $chapter['chapterId']);
						Yii::app()->cache->set($_SERVER['HTTP_REFERER'], $data);
					}
				}
			}
			else {
				$this->updateChapterHits($data[0], $data[1]);
			}
		}
	}
	
	
	/**
	 * 更新章节访问次数
	 * @param unknown_type $bookId
	 * @param unknown_type $chapterId
	 */
	public function updateChapterHits($bookId, $chapterId) {
		$sql = 'UPDATE {{module_'. $bookId .'_chapters}} SET hits=hits+1 WHERE chapterId=?';
		return Yii::app()->db->createCommand($sql)->execute(array($chapterId));
	}

	
}
